package bluetooth.game;


import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author USER
 */
public class AnimationCanvas extends GameCanvas
        implements Runnable {

    //size of one frame in the spiral image
    private static final int FRAME_WIDTH = 57;
    private static final int FRAME_HEIGHT = 53;
    private AnimationSprite spSpiral;
    private LayerManager lmgr;
    private boolean running = false;

    public AnimationCanvas() {
        //gamecanvas constructor
        super(true);

        try {
            //animated sprite
            spSpiral = new AnimationSprite(
                    Image.createImage("/spiral.png"),
                    FRAME_WIDTH, FRAME_HEIGHT);

            spSpiral.defineReferencePixel(FRAME_WIDTH / 2, FRAME_HEIGHT / 2);

            spSpiral.setRefPixelPosition(getWidth() / 2, getHeight() / 2);

            lmgr = new LayerManager();
            lmgr.append(spSpiral);
        } catch (Exception e) {
        }
    }

    public void start() {
        running = true;
        Thread t = new Thread(this);
        t.start();
    }

    public void run() {
        Graphics g = getGraphics();

        while (running) {
            //update display
            drawDisplay(g);
            try {
                Thread.sleep(150);
            } catch (InterruptedException ie) {
            }
        }
    }

    private void drawDisplay(Graphics g) {
        //animated sprite, show next frame in sequence
        spSpiral.nextFrame();

        //paint layers
        lmgr.paint(g, 0, 0);

        // flush off-screen buffer to display
        flushGraphics();
    }

    public void stop() {
        running = false;
    }
}
